home *** CD-ROM | disk | FTP | other *** search
/ Power Tools for Macintosh / Power Tools for Macintosh (SoftBit)(1992).iso / Applications / Alpha 4.01 / ACMDS / Comment Box.c < prev    next >
C/C++ Source or Header  |  1990-12-01  |  4KB  |  198 lines

  1. /******************************************************************
  2. *                                                                 *
  3. * When debugging, set the project type to application,            *
  4. * and add the ANSI project.                                       *
  5. *                                                                 *
  6. * When all your bugs are out, 'undef' DEBUG, set the project      *
  7. * type to Code Resource, with type = ACMD, name = <the name       *
  8. * of the function>, file type to 'AEXT', and the purgeable        *
  9. * flag set to true.                                               *
  10. *                                                                 *
  11. * Also, you need to remove the ANSI project from the ACMD         *
  12. * project and replace it with the ANSI-A4 project if you use      *
  13. * any functions in ANSI, such as strlen.                          *
  14. *                                                                 *
  15. * If you are not using THINK C, I'm not quite sure what you       *
  16. * should do. :-)                                                  *
  17. *                                                                 *
  18. * Change History:                                                 *
  19. * ==================================================              *
  20. * 8/6/90    Fixed bug w/ last lines.                              *
  21. *                                                                 *
  22. ******************************************************************/
  23.  
  24.  
  25. #undef    DEBUG
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #define    STAR    '*'
  32. #define    CR        '\r'
  33. #ifdef    DEBUG    
  34. #define    TABSIZE    8            /* default printf size */
  35. #else    DEBUG
  36. #define    TABSIZE    4            /* default ALPHA size */
  37. #endif    DEBUG
  38.  
  39. char    *left(char *);
  40. char    *right(char *);
  41. char    *repeatChar(char *, char, long);
  42.  
  43.  
  44. char *
  45. #ifdef    DEBUG
  46. dummy(inStr)
  47. #else    DEBUG
  48. main(inStr)
  49. #endif    DEBUG
  50. char    *inStr;
  51. {
  52.     long    lines, longest, len, i, j;
  53.     char    *ptr, *mptr, c, *lineBeg;
  54.     char    *mem;
  55.     
  56.     lines = 1;
  57.     longest = 0;
  58.     
  59.     for (ptr = inStr,len = 0; *ptr; ptr++)
  60.     {
  61.         switch (*ptr)
  62.         {
  63.             case CR:
  64.             case '\0':
  65.                 lines++;
  66.                 if (len > longest) longest = len;
  67.                 len = 0;
  68.                 break;
  69.             case '\t':
  70.                 len = (((len + 2) / TABSIZE) + 1) * TABSIZE - 2;
  71.                 break;
  72.             default:
  73.                 len++;
  74.                 break;
  75.         }
  76.     }
  77.     if (len > longest) longest = len;
  78.     if (*(ptr - 1) == '\r')
  79.     {
  80.         *(ptr - 1) = 0;
  81.         lines--;
  82.     }
  83.     
  84.     if (!(mem = NewPtr((lines + 4) * (longest + 5) + 2)))
  85.     {
  86.         SysBeep(2);
  87.         return(inStr);
  88.     }
  89.     
  90.     ptr = mem;
  91.     *ptr++ = '/';
  92.     ptr = repeatChar(ptr,STAR,longest + 3);
  93.     *ptr++ = CR;
  94.     ptr = left(ptr);
  95.     ptr = repeatChar(ptr, ' ', longest);
  96.     ptr = right(ptr);
  97.     for (i = 0, mptr = inStr; i < lines; i++, mptr++)
  98.     {
  99.         ptr = left(ptr);
  100.         lineBeg = mptr;
  101.         for (len = 0; ((c = *mptr) != CR) && c; *ptr++ = *mptr++)
  102.         {
  103.             switch (c = *mptr)
  104.             {
  105.                 case CR:
  106.                 case '\0':
  107.                     len = 0;
  108.                     break;
  109.                 case '\t':
  110.                     len = (((len + 2) / TABSIZE) + 1) * TABSIZE - 2;
  111.                     break;
  112.                 default:
  113.                     len++;
  114.             }
  115.         }
  116.         ptr = repeatChar(ptr, ' ', longest - len);
  117.         ptr = right(ptr);
  118.     }
  119.     ptr = left(ptr);
  120.     ptr = repeatChar(ptr, ' ', longest);
  121.     ptr = right(ptr);
  122.     ptr = repeatChar(ptr, STAR, longest + 3);
  123.     *ptr++ = '/';
  124.     *ptr++ = '\r';
  125.     *ptr = '\0';
  126.     
  127.     /* Must free w/ 'DisposPtr' and allocate w/ 'NewPtr' */
  128.     DisposPtr(inStr);
  129.     return(mem);
  130. }
  131.  
  132. char * left(ptr)
  133. char    *ptr;
  134. {
  135.     *ptr++ = STAR;
  136.     *ptr++ = ' ';
  137.     return(ptr);
  138. }
  139.  
  140.  
  141. char * right(ptr)
  142. char    *ptr;
  143. {
  144.     *ptr++ = ' ';
  145.     *ptr++ = STAR;
  146.     *ptr++ = CR;
  147.     return(ptr);
  148. }
  149.  
  150.  
  151. char * repeatChar(ptr, c, num)
  152. char    *ptr, c;
  153. long    num;
  154. {
  155.     long    i;
  156.     
  157.     for (i = 0; i < num; i++)
  158.         *ptr++ = c;
  159.     return(ptr);
  160. }
  161.  
  162. #ifdef    DEBUG
  163.  
  164. char * ConvertRtoN(char *theStr);
  165.  
  166. char * ConvertRtoN(theStr)
  167.     char *theStr;
  168. {
  169.     char *currentChar;
  170.     
  171.     for (currentChar = theStr;*currentChar != 0; currentChar++)
  172.         if (*currentChar == '\r')
  173.             *currentChar = '\n';
  174. }
  175.     
  176. main()
  177. {
  178.     char    *str3 = "this is nice";
  179.     char    *str4 = "nice\r> 1234    fstatus;\reasy\r";
  180.     char    *str2 = "Nice\r\tand\reasy\r";
  181.     char    *str = "(define fixLexYyC\r    set matchWords 0\r    set forward 1\r";
  182.  
  183.     char    *ret, *in;
  184.     
  185.     in = NewPtr(strlen(str) + 1);
  186.     strcpy(in, str);
  187.     
  188.     ret = dummy(in);
  189.     
  190.     ConvertRtoN(ret);                /* Convert '\r' to '\n' so we can see them in a printf */
  191.     ConvertRtoN(str);
  192.         
  193.     printf("The original is:\n\n%s", str);
  194.     printf("\n----\n\nThe result is:\n\n%s", ret);
  195.     
  196. }
  197. #endif    DEBUG
  198.